import { useMysql } from "../mysql/index.js";
import { MysqlQueryBuilder } from "../mysql/mysql-query-builder.js";
// 查询所有角色数据
export const getRoleAll = async (req, res) => {
  const sql = MysqlQueryBuilder.select("roles");
  try {
    const result = await useMysql(sql);
    res.status(200).send({
      code: 200,
      message: "查询成功",
      data: result,
    });
  } catch (err) {
    console.error("查询失败:", err);
    res.status(500).send({
      code: 500,
      message: "服务器内部错误",
    });
  }
};
// 分页查询角色
export const getRoleArr = async (req, res) => {
  const { page, pageSize, orderBy } = req.body;
  const { rowSql, countSql } = await MysqlQueryBuilder.paginate(
    "roles",
    ["*"],
    {},
    page,
    pageSize,
    orderBy
  );
  try {
    // 执行查询
    const [rowResult, countResult] = await Promise.all([
      useMysql(rowSql),
      useMysql(countSql),
    ]);
    const total = countResult[0].total;
    return res.json({
      code: 200,
      row: rowResult,
      total: total,
      message: "Success",
    });
  } catch (error) {
    return res.status(404).json({
      code: 404,
      message: "File not found",
    });
  }
};
// 更新角色
export const updataRoleById = async (req, res) => {
  const {
    role_id,
    role_name,
    role_descripition,
    status,
  } = req.body;
  // 构造 SQL 查询语句
  const sql = MysqlQueryBuilder.update(
    "roles",
    {
      role_id:role_id,
      role_name:role_name,
      role_descripition:role_descripition,
      status:status,
    },
    {
      role_id: role_id,
    }
  );
  try {
    await useMysql(sql);
    res.status(200).send({
      code: 200,
      message: "修改成功",
    });
  } catch (err) {
    res.status(500).send({
      code: 500,
      message: "服务器内部错误",
    });
  }
};
// 删除角色
export const deleteRoleById = async (req, res) => {
  const id = req.params.id;
  const sql = MysqlQueryBuilder.delete("roles", {
    role_id: id,
  });
  try {
    const result = await useMysql(sql); // 执行SQL
    res.status(200).json({
      code: 200,
      message: "删除成功",
    });
  } catch (err) {
    res.status(500).json({
      code: 500,
      message: "服务器内部错误",
      error: err.message,
    });
  }
};
// 删除角色(多id批量)
export const deleteRoleMoreById = async (req, res) => {
  const ids = req.body;
  // 构造 SQL 查询语句
  const sql = MysqlQueryBuilder.delete("roles", {
    role_id: ids,
  });
  try {
    await useMysql(sql);
    res.status(200).send({
      code: 200,
      message: "删除成功",
    });
  } catch (err) {
    res.status(500).send({
      code: 500,
      message: "服务器内部错误",
    });
  }
};
// 查找角色(单条件)
export const SearchRoleKey = async (req, res) => {
  const { SearchKey, SearchValue, page, pageSize, orderBy } = req.body;
  // 中间转化
  const TemObject = {};
  TemObject[SearchKey] = SearchValue;
  const { rowSql, countSql } = await MysqlQueryBuilder.paginate(
    "roles",
    ["*"],
    TemObject,
    page,
    pageSize,
    orderBy
  );
  try {
    // 执行查询
    const [rowResult, countResult] = await Promise.all([
      useMysql(rowSql),
      useMysql(countSql),
    ]);
    const total = countResult[0].total;
    return res.json({
      code: 200,
      row: rowResult,
      total: total,
      message: "Success",
    });
  } catch (error) {
    return res.status(404).json({
      code: 404,
      message: "File not found",
    });
  }
};
// 添加角色
export const addRole=async (req,res)=>{
  const {
    role_id,
    role_name,
    role_descripition,
    status,
  } = req.body;
  // 构造 SQL 查询语句
  const sql = MysqlQueryBuilder.insert(
    "roles",
    {
      role_id:role_id,
      role_name:role_name,
      role_descripition:role_descripition,
      status:status,
    }
  );
  try {
    await useMysql(sql);
    res.status(200).send({
      code: 200,
      message: "添加成功",
    });
  } catch (err) {
    res.status(500).send({
      code: 500,
      message: "服务器内部错误",
    });
  }
}